//
//  YQLHomeViewModel.swift
//  YQL_WeiBo
//
//  Created by apple on 2016/11/16.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import YYModel
class YQLHomeViewModel: NSObject {

    
    //保存字典转化的模型数组
    lazy var modelArr:[YQLStatusViewModel] = [YQLStatusViewModel]()
    
//    static let shared: YQLHomeViewModel = YQLHomeViewModel()
    
    func loadData(finish: @escaping (Bool) -> ()) {
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let parameters = ["access_token" : YQLOAuthViewModel.shared.userAccount?.access_token]
        DDNetworkTool.sharedManager.request(Method: .GET, urlString: urlString, parameters: parameters) { (responseObject, error) in
            
            if error != nil {
                print(error as Any)
                finish(false)
                return
            }
            finish(true)
            //将获取的网络数据转化成[string : any]
            let dict = responseObject as! [String : Any]
            
            guard let array = dict["statuses"] as? [[String : Any]]else{
                return
            }
            //创建一个临时数组存储模型
            var tempArr = [YQLStatusViewModel]()
            //变量数组,字典转模型
            for item in array{
                
                let viewModel = YQLStatusViewModel()
                //转化成模型
                let model = YQLStatusModel()
                model.yy_modelSet(with: item)
                //将模型赋值给cell的viewModel,cell的ViewModel会对数据进行进一步的加工转化
                viewModel.status = model
                //将Cell的ViewModel添加到属猪
                tempArr.append(viewModel)
            }
            //属性赋值
            self.modelArr = tempArr
            //成功获取网络数据的回调
            finish(true)
        }
        
    }
    
    
    
}
